import WebTileLayer from '@arcgis/core/layers/WebTileLayer.js';
import TileInfo from '@arcgis/core/layers/support/TileInfo.js';
import WMTSLayer from "@arcgis/core/layers/WMTSLayer.js";

export function SimpleWMTSLayer(options) {
  // const layer = new WMTSLayer({
  //   id: options.id,
  //   url: options.url, // url to the service
  //   activeLayer: {
  //     id: options.layer,
  //   }
  // });
  const layer = new WMTSLayer({
    id: 'test',
    url: 'https://geo.cqtancheng.com/gwc/service/wmts', // url to the service
    activeLayer: {
      id: 'sm-3857-256-14',
    }
  });
  return layer
}

/**
 * 创建wmts图层
 * @param options wmts图层创建配置项
 */
export function CreateWMTSLayer(options) {
  let { url, version, layerName, style, tilematrixSet } = options;
  options.urlTemplate = formatUrlTemplate(url, String(version), layerName, String(style), tilematrixSet);
  const layer = new WebTileLayer(options);
  return layer;
}

/**
 * 创建天地图图层
 * @param options
 * @returns
 */
export function CreateTDTLayer(options) {
  let { url, version, layerName, style, tilematrixSet, tiandituKey } = options;
  options.urlTemplate = formatUrlTemplate(url, String(version), layerName, String(style), tilematrixSet, tiandituKey);
  // options.tileInfo = getTianDiTuTileInfo();
  options.spatialReference = {
    wkid: 3857,
  };
  options.subDomains = ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'];
  const layer = new WebTileLayer(options);
  return layer;
}

export function CreateWebLayer(options) {
  let { url, id } = options;
  options.urlTemplate = url
  options.id = id
  options.tileInfo = getTianDiTuTileInfo();
  options.spatialReference = {
    wkid: 4490,
  };
  // options.subDomains = ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'];
  const layer = new WebTileLayer(options);
  return layer;
}

/**
 * 格式化UrlTemplate
 * @param url WMTS基础地址
 * @param version WMTS版本号
 * @param layerName WMTS图层名称
 * @param style WMTS样式名
 * @param tilematrixSet WMTS切片名
 * @param tiandituKey 天地图密钥
 * @returns
 */
function formatUrlTemplate(url, version, layerName, style, tilematrixSet, tiandituKey) {
  let res = url;
  const templateItems = {
    SERVICE: 'WMTS',
    REQUEST: 'GetTile',
    VERSION: version,
    LAYER: layerName,
    STYLE: style,
    TILEMATRIXSET: tilematrixSet,
    FORMAT: 'tiles',
    TILEMATRIX: '{level}',
    TILEROW: '{row}',
    TILECOL: '{col}',
    tk: tiandituKey,
  };
  const keys = Object.keys(templateItems);
  keys.forEach((key) => {
    res += res.indexOf('?') > -1 ? '&' : '?';
    res += `${key}=${templateItems[key]}`;
  });
  return res;
}

export function getTianDiTuTileInfo() {
  const tileInfo = new TileInfo({
    dpi: 96,
    rows: 256,
    cols: 256,
    compressionQuality: 0,
    origin: {
      x: -180,
      y: 90,
    },
    maxZoom: 20,
    minZoom: 3,
    spatialReference: {
      wkid: 3857,
    },
    lods: [
      { level: 0, levelValue: 1, scale: 295829355.45, resolution: 0.703125 },
      { level: 1, levelValue: 2, scale: 147914677.725, resolution: 0.3515625 },
      { level: 2, levelValue: 3, scale: 73957338.8625, resolution: 0.17578125 },
      { level: 3, levelValue: 4, scale: 36978669.43125, resolution: 0.087890625 },
      { level: 4, levelValue: 5, scale: 18489334.715625, resolution: 0.0439453125 },
      { level: 5, levelValue: 6, scale: 9244667.3578125, resolution: 0.02197265625 },
      { level: 6, levelValue: 7, scale: 4622333.67890625, resolution: 0.010986328125 },
      { level: 7, levelValue: 8, scale: 2311166.839453125, resolution: 0.0054931640625 },
      { level: 8, levelValue: 9, scale: 1155583.4197265625, resolution: 0.00274658203125 },
      { level: 9, levelValue: 10, scale: 577791.7098632812, resolution: 0.001373291015625 },
      { level: 10, levelValue: 11, scale: 288895.8549316406, resolution: 0.0006866455078125 },
      { level: 11, levelValue: 12, scale: 144447.9274658203, resolution: 0.00034332275390625 },
      { level: 12, levelValue: 13, scale: 72223.96373291015, resolution: 0.000171661376953125 },
      { level: 13, levelValue: 14, scale: 36111.98186645508, resolution: 0.0000858306884765625 },
      { level: 14, levelValue: 15, scale: 18055.99093322754, resolution: 0.00004291534423828125 },
      { level: 15, levelValue: 16, scale: 9027.99546661377, resolution: 0.000021457672119140625 },
      { level: 16, levelValue: 17, scale: 4513.997733306885, resolution: 0.000010728836059570312 },
      { level: 17, levelValue: 18, scale: 2256.9988666534423, resolution: 0.000005364418029785156 },
      { level: 18, levelValue: 19, scale: 1128.4994333267211, resolution: 0.000002682209014892578 },
      { level: 19, levelValue: 20, scale: 564.2497166633606, resolution: 0.000001341104507446289 },
      { level: 20, levelValue: 21, scale: 282.1248583316803, resolution: 6.705522537231445e-7 },
    ],
  });
  return tileInfo;
}
